package com.tangr.normaladapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.tangr.normaladapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tangr on 2016/6/7.
 */
public abstract class StaggerGridAdapter<T> extends RecyclerAdapter<T>{
    private List<Integer> mHeights;

    public StaggerGridAdapter(Context mContext, List<T> mList, int mLayoutId) {
        super(mContext,mList,mLayoutId);
        mHeights = new ArrayList<Integer>();
        for (int i = 0; i < mList.size(); i++) {
            mHeights.add( (int) (400 + Math.random() * 300));
        }
    }

    @Override
    public void onBindViewHolder (ViewHolder holder,int position) {
        if(holder.getItemViewType()!=HEADER_TYPE && holder.getItemViewType()!=FOOTER_TYPE && holder.getItemViewType()!=LOADMORE_TYPE){
            ViewGroup.LayoutParams lp = holder.getConvertView().getLayoutParams();
            lp.height = mHeights.get(getPosition(position));
            holder.getConvertView().setLayoutParams(lp);
        }
        super.onBindViewHolder(holder, position);
    }

    private int getPosition(int position){
        if (header_layout!=-1)
            return position - 1;
        return position;
    }

    public void notifyChanged(){
        if(mHeights.size()<mList.size()){
            for (int i = mHeights.size(); i < mList.size(); i++) {
                mHeights.add( (int) (400 + Math.random() * 300));
            }
        }
        notifyDataSetChanged();
    }

    public void notifyInsert(int positionStart, int itemCount){
        if(mHeights.size()<mList.size()){
            for (int i = mHeights.size(); i < mList.size(); i++) {
                mHeights.add( (int) (400 + Math.random() * 300));
            }
        }
        notifyItemRangeInserted(positionStart,itemCount);
    }
}
